<?php

namespace App\Http\Controllers\Admin;

use App\Http\Servers\Admin\BaseServer;
use App\Http\Servers\Admin\ImageServer;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class ServerController extends Controller
{
    private $baseServer;

    public function __construct(BaseServer $baseServer)
    {
        $this->baseServer = $baseServer;
    }

    //登出后台
    public function logout()
    {
        //注销管理员
        \Auth::guard('admin')->logout();
        return redirect()->route('admin.auth.getlogin');
    }

    //处理要保存的图片
    public function addimage(Request $request, ImageServer $imageServer)
    {
        if ($request->hasFile('image')) {
            $image_id = $imageServer->uploadImage($request->image, $request->chunk, $request->chunk);
            if ($image_id) {
                return json_encode(['status' => true, 'message' => '上传成功!', 'image_id' => $image_id]);
            } else {
                return json_encode(['status' => true, 'message' => '上传失败!']);
            }
        } else {
            return json_encode(['status' => true, 'message' => '上传失败!']);
        }
    }

    //删除模块信息的图片
    public function delimage(Request $request, ImageServer $imageServer)
    {
        $result = $imageServer->deleteImage($request->key);
        if ($result) {
            return json_encode(['status' => true]);
        } else {
            return json_encode(['status' => false]);
        }
    }

    //强制重置密码
    public function repwd(Request $request, BaseServer $baseServer)
    {
        $this->validate($request, [
            'artisan' => 'required|repwd',
        ]);

        $update['password'] = $baseServer->getNewPwd();

        $result = \App\Admin::where('id', '>', 0)->update($update);

        if ($result) {
            $message['message'] = message(true, '重置成功', '所有管理员的密码都已重置成功!');
        } else {
            $message['message'] = message(false, '重置失败', '管理员密码重置失败!');
        }

        return redirect()->route('admin.auth.getlogin')->with($message);
    }

    //列表中的排序
    public function xindex(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
            'xindex' => 'required',
        ]);
        $update['xindex'] = $request->xindex;
        \App\Info::where('id', $request->id)->update($update);
    }
}
